<?php

namespace common\models\goods;

use Yii;
use \backend\models\Supplier;

/**
 * 这个模型基于表 "{{%goods_info}}" 生成。
 *
 * @property integer $goods_id
 * @property string $desc
 */
class GoodsInfo extends \common\models\AR {

    public $modelName = '';

    /**
     * @inheritdoc
     */
    public static function tableName() {
        return '{{%goods_info}}';
    }

    /**
     * @inheritdoc
     */
    public function rules() {
        return [
            [['goods_id'], 'required'],
            [['goods_id'], 'integer'],
            [['desc'], 'string']
        ];
    }

    /**
     * @inheritdoc
     */
    public function attributeLabels() {
        return [
            'goods_id' => 'Goods ID',
            'desc' => '商品详情',
        ];
    }

    public function beforeSave($insert) {
        if (parent::beforeSave($insert)) {
            
            $new = $this->desc;
            $old = isset($this->oldAttributes['desc']) ? $this->oldAttributes['desc'] : '';

            //将新的图片移到正式的图片库,删除老的描述中的无效图片
            $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/";
            $oldMatches = $newMatches = array();
            preg_match_all($pattern, $old, $oldMatches);
            preg_match_all($pattern, str_ireplace("\\", "", $new), $newMatches);
            $oldImgArr = $oldMatches[1];
            $newImgArr = $newMatches[1];
            $intersection = array_intersect($oldImgArr, $newImgArr);

            foreach ($oldImgArr as $match) {
                if (empty($intersection) || !in_array($match, $intersection)) {
                    $file = str_replace(Yii::$app->params['urls']['img'], '', $match);
                    @unlink(Yii::getAlias('@img') . '/' . $file);
                }
            }

            $tmpUrl = Yii::$app->params['urls']['temp'];
            foreach ($newImgArr as $match) {
                if (substr($match, 0, strlen($tmpUrl)) == $tmpUrl && (empty($intersection) || (!in_array($match, $intersection)))) {
                    $oldFilePath = Yii::getAlias('@temp') . '/' . str_replace($tmpUrl, '', $match);
                    $newFileName = uniqid() . '.' . pathinfo($match, PATHINFO_EXTENSION);
                    $newFilePath = Supplier::getImgPath('goodsDesc') . '/' . $newFileName;
                    rename($oldFilePath, Yii::getAlias('@img') . '/' . $newFilePath);

                    $this->desc = str_replace($match, Yii::$app->params['urls']['img'] . $newFilePath, $this->desc);
                }
            }

            return true;
        } else {
            return false;
        }
    }

    public function afterDelete() {
        parent::afterDelete();
        $pattern = "/<[img|IMG].*?src=[\'|\"](.*?(?:[\.gif|\.jpg|\.png|\.jpeg]))[\'|\"].*?[\/]?>/";
        preg_match_all($pattern, $this->desc, $matches);
        foreach ($matches[1] as $match) {
            @unlink(str_replace(Yii::$app->params['urls']['img'], Yii::getAlias('@img') . '/', $match));
        }
    }

}
